package com.ocean.web.controller.product;

import java.io.FileNotFoundException;
import java.util.List;
import java.util.Map;

import com.ocean.common.core.domain.entity.SysUser;
import com.ocean.common.utils.DictUtils;
import com.ocean.common.utils.ShiroUtils;
import com.ocean.common.utils.YamlUtil;
import com.ocean.nc.domain.ONcFile;
import com.ocean.nc.service.IONcFileService;
import com.ocean.product.domain.OProduct;
import com.ocean.product.service.IOProductService;
import com.ocean.system.service.ISysRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ocean.common.annotation.Log;
import com.ocean.common.enums.BusinessType;
import com.ocean.productRelease.domain.OProductRelease;
import com.ocean.productRelease.service.IOProductReleaseService;
import com.ocean.common.core.controller.BaseController;
import com.ocean.common.core.domain.AjaxResult;
import com.ocean.common.utils.poi.ExcelUtil;
import com.ocean.common.core.page.TableDataInfo;

/**
 * 产品发布记录Controller
 *
 * @author ocean
 * @date 2023-02-13
 */
@Controller
@RequestMapping("/productRelease/productRelease")
public class OProductReleaseController extends BaseController
{
    private String prefix = "product/productRelease";

    @Autowired
    private IOProductReleaseService oProductReleaseService;
    @Autowired
    private ISysRoleService sysRoleService;
    @Autowired
    private IOProductService productService;
    @Autowired
    private IONcFileService ncFileService;

    @RequiresPermissions("productRelease:productRelease:view")
    @GetMapping()
    public String productRelease()
    {
        return prefix + "/productRelease";
    }

    /**
     * 查询产品发布记录列表
     */
    @RequiresPermissions("productRelease:productRelease:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OProductRelease oProductRelease)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        //当前登录用户是否是单位管理员
        if (sysRoleService.isDeptAdmin(sysUser)){
            oProductRelease.setDepartId(sysUser.getDeptId().toString());
        }
        startPage();
        List<OProductRelease> list = oProductReleaseService.selectOProductReleaseList(oProductRelease);
        return getDataTable(list);
    }

    /**
     * 导出产品发布记录列表
     */
    @RequiresPermissions("productRelease:productRelease:export")
    @Log(title = "产品发布记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OProductRelease oProductRelease)
    {
        List<OProductRelease> list = oProductReleaseService.selectOProductReleaseList(oProductRelease);
        ExcelUtil<OProductRelease> util = new ExcelUtil<OProductRelease>(OProductRelease.class);
        return util.exportExcel(list, "产品发布记录数据");
    }

    /**
     * 新增产品发布记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存产品发布记录
     */
    @RequiresPermissions("productRelease:productRelease:add")
    @Log(title = "产品发布记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(OProductRelease oProductRelease)
    {
        return toAjax(oProductReleaseService.insertOProductRelease(oProductRelease));
    }

    /**
     * 修改产品发布记录
     */
    @RequiresPermissions("productRelease:productRelease:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        OProductRelease oProductRelease = oProductReleaseService.selectOProductReleaseById(id);
        mmap.put("oProductRelease", oProductRelease);
        return prefix + "/edit";
    }

    /**
     * 修改保存产品发布记录
     */
    @RequiresPermissions("productRelease:productRelease:edit")
    @Log(title = "产品发布记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(OProductRelease oProductRelease)
    {
        return toAjax(oProductReleaseService.updateOProductRelease(oProductRelease));
    }

    /**
     * 删除产品发布记录
     */
    @RequiresPermissions("productRelease:productRelease:remove")
    @Log(title = "产品发布记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try {
            oProductReleaseService.deleteOProductRelease(ids);
            return AjaxResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("删除失败");
        }
    }

    /**
     * 产品发布记录详情
     */
    @RequiresPermissions("productRelease:productRelease:detail")
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") String id, ModelMap mmap)
    {
        OProductRelease oProductRelease = oProductReleaseService.selectOProductReleaseById(id);
        mmap.put("oProductRelease", oProductRelease);
        OProduct oProduct = productService.selectOProductById(oProductRelease.getProductId());
        mmap.put("product",oProduct);
        ONcFile oNcFile = ncFileService.selectONcFileById(oProductRelease.getNcFileId());
        mmap.put("ncFile",oNcFile);
        return prefix + "/detail";
    }

    /**
     * 产品发布
     * @param ids 产品发布记录id
     */
    @RequiresPermissions("productRelease:productRelease:release")
    @PostMapping("releaseProduct")
    @ResponseBody
    public AjaxResult releaseProduct(String ids){
        try {
            oProductReleaseService.releaseProduct(ids);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("产品发布失败");
        }
        return AjaxResult.success();
    }

    @RequiresPermissions("productRelease:productRelease:archiving")
    @PostMapping("archiving")
    @ResponseBody
    public AjaxResult archiving(){
        ncFileService.autoFile();
        return AjaxResult.success("归档成功");
    }

}
